<template>
  <!-- 页面-应用配置-编辑 -->
  <div class="appConfig-page">
    <div class="config-l">
      <ul>
        <li v-for="item in labelList"
        :key="item.key"
        @click="changeType(item.key)"
        :class="{'is-active': activeName === item.key || (item.key==='rwork' && activeName.includes('rwork'))}">
          {{ item.label }}
        </li>
      </ul>
    </div>
    <div class="config-r">
      <div class="config-from">
        <Form
          :model="fromData[activeName]" ref="appConfigForm"
          :rules="rule"
          :label-width="150">
          <!-- ECSB集成配置 / AI平台集成配置-->
          <div v-if="activeName === 'ecsb' || activeName === 'ai'" key="ecsbAndAi">
            <Row :gutter="32">
              <Col span="12">
                <Form-item prop="enabled" label="是否启用">
                 <i-switch :true-value="1" :false-value="0" v-model="fromData[activeName].enabled">
                      <span slot="open">{{$t('module.appManage.open')}}</span>
                      <span slot="close">{{$t('module.appManage.close')}}</span>
                    </i-switch>
                </Form-item>
              </Col>
              <Col span="12" v-if="activeName === 'ai'" key="ai-commonEcsbAccount">
                <Form-item prop="commonEcsbAccount" label="延用ECSB集成配置">
                  <Radio v-model="fromData[activeName].commonEcsbAccount" @on-change="ecsbAccountByAiChange">是</Radio>
                  <Radio v-model="fromData[activeName].commonEcsbAccount" @on-change="ecsbAccountByAiChange" :true-value="false" :false-value="true">否</Radio>
                </Form-item>
              </Col>
              <Col span="12" v-else>
                <Form-item prop="appToken" label="调用方应用授权令牌" key="appToken">
                  <Input
                    placeholder="请输入调用方应用授权令牌"
                    v-model.trim="fromData[activeName].appToken"
                    maxlength="32"
                  />
                </Form-item>
              </Col>

            </Row>
            <Row :gutter="32" v-if="isCustomByAi">
              <Col span="12">
                <Form-item prop="appSubId" label="调用方的应用编码">
                  <Input
                    placeholder="请输入调用方的应用编码"
                    v-model.trim="fromData[activeName].appSubId"
                    maxlength="16"
                  />
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="partnerId" label="调用方的组织编码">
                  <Input
                    placeholder="请输入调用方的组织编码"
                    v-model.trim="fromData[activeName].partnerId"
                    maxlength="16"
                  />
                </Form-item>
                <!-- <Form-item prop="apiID" label="被调用的API编码">
                  <Input
                    placeholder="请输入被调用的API编码"
                    v-model.trim="fromData[activeName].apiID"
                    maxlength="128"
                  />
                </Form-item> -->
              </Col>
            </Row>
            <!-- <Row :gutter="32" v-if="isCustomByAi">
              <Col span="12">
                <Form-item prop="apiVersion" label="被调用的API版本">
                  <Input
                    placeholder="请输入被调用的API版本"
                    v-model.trim="fromData[activeName].apiVersion"
                    maxlength="32"
                  />
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="partnerId" label="调用方的组织编码">
                  <Input
                    placeholder="请输入调用方的组织编码"
                    v-model.trim="fromData[activeName].partnerId"
                    maxlength="16"
                  />
                </Form-item>
              </Col>
            </Row> -->
            <Row :gutter="32" v-if="isCustomByAi">
              <Col span="12">
                <Form-item prop="sysId" label="调用方的系统编码">
                  <Input
                    placeholder="请输入调用方的系统编码"
                    v-model.trim="fromData[activeName].sysId"
                    maxlength="16"
                  />
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="sign" label="请求报文的签名">
                  <Input
                    placeholder="请输入请求报文的签名"
                    v-model.trim="fromData[activeName].sign"
                  />
                </Form-item>
              </Col>
            </Row>
            <Row :gutter="32" v-if="activeName === 'ai' && isCustomByAi">
              <Col span="12">
                <Form-item prop="appToken" label="调用方应用授权令牌" key="ai-appToken">
                  <Input
                    placeholder="请输入调用方应用授权令牌"
                    v-model.trim="fromData[activeName].appToken"
                    maxlength="32"
                  />
                </Form-item>
              </Col>
            </Row>
          </div>
          <!-- 主数据集成配置  -->
          <div v-if="activeName === 'mdm'" key="mdm">
            <Row :gutter="32">
              <Col span="12">
                <Form-item prop="enabled" label="是否启用">
                 <i-switch :true-value="1" :false-value="0" v-model="fromData[activeName].enabled">
                    <span slot="open">{{$t('module.appManage.open')}}</span>
                    <span slot="close">{{$t('module.appManage.close')}}</span>
                  </i-switch>
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="enableMdm" label="开启主数据同步">
                 <i-switch :true-value="1" :false-value="0" v-model="fromData[activeName].enableMdm">
                    <span slot="open">{{$t('module.appManage.open')}}</span>
                    <span slot="close">{{$t('module.appManage.close')}}</span>
                  </i-switch>
                </Form-item>
              </Col>
            </Row>
            <Row :gutter="32">
              <Col span="12">
                <Form-item prop="mergeMdmOrg" label="合并主数据到组织架构">
                 <i-switch :true-value="1" :false-value="0" v-model="fromData[activeName].mergeMdmOrg">
                    <span slot="open">{{$t('module.appManage.open')}}</span>
                    <span slot="close">{{$t('module.appManage.close')}}</span>
                  </i-switch>
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="synchCycle" label="数据同步周期时间设置(小时)">
                  <InputNumber style="width: 100%;" :precision="0" :max="10000" :min="1" :step="1" v-model="fromData[activeName].synchCycle" />
                </Form-item>
              </Col>
            </Row>
            <Row :gutter="32">
              <Col span="12">
                <Form-item prop="orgFields" label="组织数据">
                  <!-- disabled @click.native="onSelect($event)" class="custom-show-only-select" -->
                  <Select v-model="fromData[activeName].orgFields" :max-tag-count="2" disabled @click.native="onSelect('orgFields')" class="custom-show-only-select" multiple>
                    <Option v-for="item in orgTemplateOption" :value="item.value" :key="item.value">{{ item.label }}</Option>
                  </Select>
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="employeeFields" label="员工数据">
                  <Select v-model="fromData[activeName].employeeFields" :max-tag-count="2" multiple  disabled @click.native="onSelect('employeeFields')" class="custom-show-only-select">
                    <Option v-for="item in staffTemplateOption" :value="item.value" :key="item.value">{{ item.label }}</Option>
                  </Select>
                </Form-item>
              </Col>
            </Row>
          </div>
          <!-- 润工作 -->
          <Tabs v-model="activeNameByRwork" @on-click="changeType" v-if="activeName === 'rworkRobotNotice' || activeName === 'rworkLightapp'" key="rwork">
            <TabPane label="润工作机器人通知" name="rworkRobotNotice">
              <!-- 润工作集成配置  润工作机器人通知-->
              <div v-if="activeName === 'rworkRobotNotice'">
                <Row :gutter="32">
                  <Col span="12">
                    <Form-item prop="enabled" label="是否启用">
                    <i-switch :true-value="1" :false-value="0" v-model="fromData[activeName].enabled">
                          <span slot="open">{{$t('module.appManage.open')}}</span>
                          <span slot="close">{{$t('module.appManage.close')}}</span>
                        </i-switch>
                    </Form-item>
                  </Col>
                  <Col span="12">
                    <Form-item prop="commonEcsbAccount" label="延用ECSB集成配置">
                      <!-- <RadioGroup v-model="fromData[activeName].commonEcsbAccount">
                        <Radio :label="true">是</Radio>
                        <Radio :label="false">否</Radio>
                    </RadioGroup> -->
                    <Radio v-model="fromData[activeName].commonEcsbAccount" @on-change="ecsbAccountByAiChange">是</Radio>
                    <Radio v-model="fromData[activeName].commonEcsbAccount" @on-change="ecsbAccountByAiChange" :true-value="false" :false-value="true">否</Radio>

                    </Form-item>
                  </Col>
                </Row>
                <Row :gutter="32">
                  <Col span="12">
                    <Form-item prop="appAccount" label="润工作应用ID">
                      <Input
                        placeholder="请输入润工作应用账户"
                        v-model.trim="fromData[activeName].appAccount"
                        maxlength="64"
                      />
                    </Form-item>
                  </Col>
                  <Col span="12">
                    <Form-item prop="appSecret" label="润工作应用Secret">
                      <Input
                        placeholder="请输入润工作应用Secret"
                        v-model.trim="fromData[activeName].appSecret"
                        maxlength="64"
                      />
                    </Form-item>
                  </Col>
                </Row>
                <Row :gutter="32" v-if="isCustomByAi">
                  <Col span="12">
                    <Form-item prop="appSubId" label="调用方的应用编码">
                      <Input
                        placeholder="请输入调用方的应用编码"
                        v-model.trim="fromData[activeName].appSubId"
                        maxlength="16"
                      />
                    </Form-item>
                  </Col>
                  <Col span="12">
                    <Form-item prop="partnerId" label="调用方的组织编码">
                      <Input
                        placeholder="请输入调用方的组织编码"
                        v-model.trim="fromData[activeName].partnerId"
                        maxlength="16"
                      />
                    </Form-item>
                    <!-- <Form-item prop="apiID" label="被调用的API编码">
                      <Input
                        placeholder="请输入被调用的API编码"
                        v-model.trim="fromData[activeName].apiID"
                        maxlength="128"
                      />
                    </Form-item> -->
                  </Col>
                </Row>
                <!-- <Row :gutter="32" v-if="isCustomByAi">
                  <Col span="12">
                    <Form-item prop="apiVersion" label="被调用的API版本">
                      <Input
                        placeholder="请输入被调用的API版本"
                        v-model.trim="fromData[activeName].apiVersion"
                        maxlength="32"
                      />
                    </Form-item>
                  </Col>
                  <Col span="12">
                    <Form-item prop="partnerId" label="调用方的组织编码">
                      <Input
                        placeholder="请输入调用方的组织编码"
                        v-model.trim="fromData[activeName].partnerId"
                        maxlength="16"
                      />
                    </Form-item>
                  </Col>
                </Row> -->
                <Row :gutter="32" v-if="isCustomByAi">
                  <Col span="12">
                    <Form-item prop="sysId" label="调用方的系统编码">
                      <Input
                        placeholder="请输入调用方的系统编码"
                        v-model.trim="fromData[activeName].sysId"
                        maxlength="16"
                      />
                    </Form-item>
                  </Col>
                  <Col span="12">
                    <Form-item prop="appToken" label="调用方应用授权令牌" key="ai-appToken">
                      <Input
                        placeholder="请输入调用方应用授权令牌"
                        v-model.trim="fromData[activeName].appToken"
                        maxlength="32"
                      />
                    </Form-item>
                  </Col>
                </Row>
                <Row :gutter="32" v-if="isCustomByAi">
                  <Col span="12">
                    
                    <Form-item prop="sign" label="请求报文的签名">
                      <Input
                        placeholder="请输入请求报文的签名"
                        v-model.trim="fromData[activeName].sign"
                      />
                    </Form-item>
                  </Col>
                </Row>
              </div>
            </TabPane>
            <TabPane label="润工作轻应用集成" name="rworkLightapp">
               <!-- 润工作集成配置  润工作轻应用集成-->
              <div v-if="activeName === 'rworkLightapp'" key="rworkLightapp">
                <Row :gutter="32">
                  <Col span="12">
                    <Form-item prop="enabled" label="是否启用">
                    <i-switch :true-value="1" :false-value="0" v-model="fromData[activeName].enabled">
                          <span slot="open">{{$t('module.appManage.open')}}</span>
                          <span slot="close">{{$t('module.appManage.close')}}</span>
                        </i-switch>
                    </Form-item>
                  </Col>
                  <Col span="12">
                    <Form-item prop="commonEcsbAccount" label="延用ECSB集成配置">
                      <Radio v-model="fromData[activeName].commonEcsbAccount" @on-change="ecsbAccountByAiChange">是</Radio>
                      <Radio v-model="fromData[activeName].commonEcsbAccount" @on-change="ecsbAccountByAiChange" :true-value="false" :false-value="true">否</Radio>
                    </Form-item>
                  </Col>
                </Row>
                <Row :gutter="32">
                  <Col span="12">
                    <Form-item prop="appAccount" label="润工作应用ID">
                      <Input
                        placeholder="请输入润工作应用账户"
                        v-model.trim="fromData[activeName].appAccount"
                        maxlength="64"
                      />
                    </Form-item>
                  </Col>
                  <Col span="12">
                    <Form-item prop="appSecret" label="润工作应用Secret">
                      <Input
                        placeholder="请输入润工作应用Secret"
                        v-model.trim="fromData[activeName].appSecret"
                        maxlength="64"
                      />
                    </Form-item>
                  </Col>
                </Row>
                <!-- <Row :gutter="32">
                  <Col span="12">
                    <Form-item prop="ecsbAccount" label="ECSB账号">
                      <Input
                        placeholder="请输入ECSB账号"
                        v-model.trim="fromData[activeName].ecsbAccount"
                        maxlength="64"
                      />
                    </Form-item>
                  </Col>
                  <Col span="12">
                    <Form-item prop="ecsbToken" label="ECSB Token">
                      <Input
                        placeholder="请输入ECSB Token"
                        v-model.trim="fromData[activeName].ecsbToken"
                        maxlength="64"
                      />
                    </Form-item>
                  </Col>
                </Row> -->
                <!-- <Row :gutter="32">
                  <Col span="12">
                    <Form-item prop="appSignSecret" label="应用签名密钥">
                      <Input
                        placeholder="请输入应用签名密钥"
                        v-model.trim="fromData[activeName].appSignSecret"
                        maxlength="64"
                      />
                    </Form-item>
                  </Col>
                  <Col span="12" v-if="isCustomByAi">
                    <Form-item prop="appToken" label="调用方应用授权令牌" key="ai-appToken">
                      <Input
                        placeholder="请输入调用方应用授权令牌"
                        v-model.trim="fromData[activeName].appToken"
                        maxlength="32"
                      />
                    </Form-item>
                  </Col>
                </Row> -->
                <Row :gutter="32" v-if="isCustomByAi">
                  <Col span="12">
                    <Form-item prop="appToken" label="调用方应用授权令牌" key="ai-appToken">
                      <Input
                        placeholder="请输入调用方应用授权令牌"
                        v-model.trim="fromData[activeName].appToken"
                        maxlength="32"
                      />
                    </Form-item>
                  </Col>
                  <Col span="12">
                    <Form-item prop="appSubId" label="调用方的应用编码">
                      <Input
                        placeholder="请输入调用方的应用编码"
                        v-model.trim="fromData[activeName].appSubId"
                        maxlength="16"
                      />
                    </Form-item>
                  </Col>
                </Row>
                <Row :gutter="32" v-if="isCustomByAi">
                  <Col span="12">
                    <Form-item prop="partnerId" label="调用方的组织编码">
                      <Input
                        placeholder="请输入调用方的组织编码"
                        v-model.trim="fromData[activeName].partnerId"
                        maxlength="16"
                      />
                    </Form-item>
                  </Col>
                  <Col span="12">
                    <Form-item prop="sysId" label="调用方的系统编码">
                      <Input
                        placeholder="请输入调用方的系统编码"
                        v-model.trim="fromData[activeName].sysId"
                        maxlength="16"
                      />
                    </Form-item>
                  </Col>
                </Row>
                <Row :gutter="32" v-if="isCustomByAi">
                  
                  <Col span="12">
                    <Form-item prop="sign" label="请求报文的签名">
                      <Input
                        placeholder="请输入请求报文的签名"
                        v-model.trim="fromData[activeName].sign"
                      />
                    </Form-item>
                  </Col>
                </Row>
              </div>
            </TabPane>
          </Tabs>

          <!-- 云存储集成配置 -->
          <div v-if="activeName === 'cloudStorage'" key="cloudStorage">
            <Row :gutter="32">
              <Col span="12">
                <Form-item prop="enabled" label="是否启用">
                 <i-switch :true-value="1" :false-value="0" v-model="fromData[activeName].enabled">
                    <span slot="open">{{$t('module.appManage.open')}}</span>
                    <span slot="close">{{$t('module.appManage.close')}}</span>
                  </i-switch>
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="secretKey" label="存储密匙" key="secretKey">
                  <Input
                    placeholder="请输入存储密匙"
                    v-model.trim="fromData[activeName].secretKey"
                    maxlength="64"
                  />
                </Form-item>
              </Col>
            </Row>
            <Row :gutter="32">
              <Col span="12">
                <Form-item prop="ipaddress" label="存储账号">
                  <Input
                    placeholder="请输入存储账号"
                    v-model.trim="fromData[activeName].ipaddress"
                    maxlength="64"
                  />
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="bucketName" label="存储空间名称">
                  <Input
                    placeholder="请输入存储空间名称"
                    v-model.trim="fromData[activeName].bucketName"
                    maxlength="64"
                  />
                </Form-item>
              </Col>
            </Row>
            <Row :gutter="32">
              <Col span="12">
                <Form-item prop="accessKeyID" label="存储密码">
                  <Input
                    placeholder="请输入存储密码"
                    v-model.trim="fromData[activeName].accessKeyID"
                    maxlength="64"
                  />
                </Form-item>
              </Col>
            </Row>
          </div>
          <!-- 调度管理集成配置 -->
          <div v-if="activeName === 'schedulingCenter'" key="schedulingCenter">
            <Row :gutter="32">
              <Col span="12">
                <Form-item prop="enabled" label="是否启用">
                 <i-switch :true-value="1" :false-value="0" v-model="fromData[activeName].enabled">
                    <span slot="open">{{$t('module.appManage.open')}}</span>
                    <span slot="close">{{$t('module.appManage.close')}}</span>
                  </i-switch>
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="executorAppname" label="执行器名称">
                  <Input
                    placeholder="请输入执行器名称"
                    v-model.trim="fromData[activeName].executorAppname"
                    maxlength="64"
                  />
                </Form-item>
              </Col>
            </Row>
            <Row :gutter="32">
              <Col span="12">
                <Form-item prop="executorIp" label="执行器IP">
                  <Input
                    placeholder="请输入执行器IP"
                    v-model.trim="fromData[activeName].executorIp"
                    maxlength="256"
                  />
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="executorLogpath" label="执行器日志存储路径">
                  <Input
                    placeholder="请输入执行器日志存储路径"
                    v-model.trim="fromData[activeName].executorLogpath"
                    maxlength="526"
                  />
                </Form-item>
              </Col>
            </Row>
            <Row :gutter="32">
              <Col span="12">
                <Form-item prop="executorLogretentiondays" label="日志保存天数">
                  <InputNumber style="width: 100%;" :precision="0" :max="730" :min="1" :step="1" v-model="fromData[activeName].executorLogretentiondays" />
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="admiadminAddresses" label="调度服务地址">
                  <Input
                    placeholder="请输入调度服务地址"
                    v-model.trim="fromData[activeName].admiadminAddresses"
                    maxlength="1024"
                  />
                </Form-item>
              </Col>
            </Row>
            <Row :gutter="32">
              <Col span="12">
                <Form-item prop="accessToken" label="执行器通行令牌">
                  <Input
                    placeholder="请输入执行器通行令牌"
                    v-model.trim="fromData[activeName].accessToken"
                    maxlength="64"
                  />
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="executorPort" label="执行器端口">
                  <Input
                    placeholder="请输入执行器端口"
                    v-model.trim="fromData[activeName].executorPort"
                    maxlength="10"
                  />
                </Form-item>
              </Col>
            </Row>
          </div>
          <!-- 消息中心管理配置 -->
          <div v-if="activeName === 'messageCenter'" key="messageCenter">
            <Row :gutter="32">
              <Col span="12">
                <Form-item prop="enabled" label="是否启用">
                 <i-switch :true-value="1" :false-value="0" v-model="fromData[activeName].enabled">
                    <span slot="open">{{$t('module.appManage.open')}}</span>
                    <span slot="close">{{$t('module.appManage.close')}}</span>
                  </i-switch>
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="appId" label="应用ID">
                  <Input
                    placeholder="请输入应用ID"
                    v-model.trim="fromData[activeName].appId"
                    maxlength="64"
                  />
                </Form-item>
              </Col>
            </Row>
            <Row :gutter="32">
              <Col span="12">
                <Form-item prop="tenantId" label="租户ID">
                  <Input
                    placeholder="请输入租户ID"
                    v-model.trim="fromData[activeName].tenantId"
                    maxlength="64"
                  />
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="signCode" label="签名编码">
                  <Input
                    placeholder="请输入应用签名密匙"
                    v-model.trim="fromData[activeName].signCode"
                    maxlength="64"
                  />
                </Form-item>
              </Col>
              <!-- <Col span="12">
                <Form-item prop="sendType" label="发送类别">
                  <Select v-model="fromData[activeName].sendType" placeholder="请选择发送类别">
                    <Option value="MASSAGE">短信</Option>
                    <Option value="EMAIL">邮件</Option>
                    <Option value="APP">App通知</Option>
                    <Option value="ALL">全部</Option>
                  </Select>
                </Form-item>
              </Col> -->
            </Row>
            <Row :gutter="32">
              <Col span="12">
                <Form-item prop="appKey" label="访问令牌">
                  <Input
                    placeholder="请输入访问令牌"
                    v-model.trim="fromData[activeName].appKey"
                    maxlength="64"
                  />
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="appSecret" label="签名密钥" :required="false" :rules="[]" key="msgc-appSecret">
                  <Input
                    placeholder="请输入签名密钥"
                    v-model.trim="fromData[activeName].appSecret"
                    maxlength="64"
                  />
                </Form-item>
              </Col>
            </Row>
            <Row :gutter="32">
              <Col span="12">
                <Form-item prop="submiter" label="消息中心账户ID">
                  <Input
                    placeholder="请输入消息中心账户ID"
                    v-model.trim="fromData[activeName].submiter"
                    maxlength="64"
                  />
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="url" label="消息中心服务地址" key="msgc-url">
                  <Input
                    placeholder="请输入消息中心服务地址"
                    v-model.trim="fromData[activeName].url"
                    maxlength="526"
                  />
                </Form-item>
              </Col>
            </Row>
            <Row :gutter="32">
              
            </Row>
          </div>
            <!-- 文档中台集成配置 -->
          <div v-if="activeName === 'docCenter'" key="docCenter">
            <Row :gutter="32">
              <Col span="12">
                <Form-item prop="enabled" label="是否启用">
                  <i-switch :true-value="1" :false-value="0" v-model="fromData[activeName].enabled">
                    <span slot="open">{{$t('module.appManage.open')}}</span>
                    <span slot="close">{{$t('module.appManage.close')}}</span>
                  </i-switch>
                </Form-item>
              </Col>
              <Col span="12" key="docCenter-commonEcsbAccount">
                <Form-item prop="commonEcsbAccount" label="延用ECSB集成配置">
                  <Radio v-model="fromData[activeName].commonEcsbAccount" @on-change="ecsbAccountByAiChange">是</Radio>
                  <Radio v-model="fromData[activeName].commonEcsbAccount" @on-change="ecsbAccountByAiChange" :true-value="false" :false-value="true">否</Radio>
                </Form-item>
              </Col>
            </Row>
            <Row :gutter="32" v-if="isCustomByAi">
              <Col span="12">
                <Form-item prop="appToken" label="调用方应用授权令牌" key="docCenter-appToken">
                  <Input
                    placeholder="请输入调用方应用授权令牌"
                    v-model.trim="fromData[activeName].appToken"
                    maxlength="32"
                  />
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="appSubId" label="调用方的应用编码" key="docCenter-appSubId">
                  <Input
                    placeholder="请输入调用方的应用编码"
                    v-model.trim="fromData[activeName].appSubId"
                    maxlength="16"
                  />
                </Form-item>
              </Col>
            </Row>
            <Row :gutter="32" v-if="isCustomByAi">
              <Col span="12">
                <Form-item prop="partnerId" label="调用方的组织编码" key="docCenter-partnerId">
                  <Input
                    placeholder="请输入调用方的组织编码"
                    v-model.trim="fromData[activeName].partnerId"
                    maxlength="16"
                  />
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="sysId" label="调用方的系统编码" key="docCenter-sysId">
                  <Input
                    placeholder="请输入调用方的系统编码"
                    v-model.trim="fromData[activeName].sysId"
                    maxlength="16"
                  />
                </Form-item>
              </Col>
            </Row>
            <Row :gutter="32" v-if="isCustomByAi" style="border-bottom: 1px solid #d8d8d8">
              
              <Col span="12">
                <Form-item prop="sign" label="请求报文的签名" key="docCenter-sign">
                  <Input
                    placeholder="请输入请求报文的签名"
                    v-model.trim="fromData[activeName].sign"
                  />
                </Form-item>
              </Col>
            </Row>
            <Row :gutter="32">
              <Col span="12">
                <Form-item
                prop="appId"
                :rules="{required: true, message: '应用账号不能为空', trigger: 'blur'}"
                label="应用账号">
                  <Input
                    placeholder="请输入应用账号"
                    maxlength="48"
                    v-model.trim="fromData[activeName].appId"
                  />
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item
                  prop="appKey"
                  :rules="{required: true, message: '应用token不能为空', trigger: 'blur'}"
                  label="应用token">
                  <Input
                    placeholder="请输入应用token"
                    v-model.trim="fromData[activeName].appKey"
                    maxlength="48"
                  />
                </Form-item>
              </Col>
            </Row>
            <Row :gutter="32">
              <Col span="12">
                <Form-item prop="fileSize" label="文件传输大小上限">
                  <Input 
                  v-model.trim="fromData[activeName].fileSize"
                  maxlength="8"
                  >
                    <template #append>
                      <Select v-model="fromData[activeName].unit" style="width: 70px">
                        <Option value="Byte">Byte</Option>
                        <Option value="KB">KB</Option>
                        <Option value="MB">MB</Option>
                        <!-- <Option value="GB">GB</Option> -->
                      </Select>
                    </template>
                  </Input>
                </Form-item>
              </Col>
              <Col span="12">
                <Form-item prop="storageType" label="存储方式">
                  <Select v-model="fromData[activeName].storageType" placeholder="请选择存储方式">
                    <Option :value="storageTypeLocal">本地存储</Option>
                    <Option :value="storageTypeS3">S3存储</Option>
                  </Select>
                </Form-item>
              </Col>
            </Row>
            <Row :gutter="32" v-if="fromData[activeName].storageType == storageTypeLocal">
              <Col span="24">
                <Form-item
                prop="localPath"
                label="本地存储路径">
                  <Input
                    placeholder="请输入本地存储路径"
                    v-model.trim="fromData[activeName].localPath"
                    maxlength="64"
                  />
                </Form-item>
              </Col>
            </Row>

          </div>
        </Form>
      </div>
      <div class="save-btn-box">
        <RPButton optType="save" :loading="saveLoading" @click="onSave" />
      </div>
    </div>
  </div>
</template>
<script>
import locales from "./locales.js";
import { CoreModuleName } from "@/const";
import {addAppConfig, getAppConfigByType, updateAppConfig} from "@/api/applicationApi.js";
import { getTemplateDetail } from "@/api/template.js"
import selectField from "./selectField.vue";

export default {
  name: "appConfig",
  mixins: [locales],
  components: {},
  props: {
    appInfo: {
      type: Object,
      default() {
        return {};
      }
    },
    readOnly: {
      type: Boolean,
      default: false
    },
    row: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  computed: {
  },
  data() {
    const STOREAGE_TYPE_LOCAL = 'local'
    const STOREAGE_TYPE_S3 = 'amazons3'
    const { userInfo } = $store.state[CoreModuleName];
    const ECSBParam = {
          appToken: '', // 调用方应用授权令牌
          appSubId: '', // 调用方的应用编码
          apiID: '', // 被调用的API编码
          apiVersion: '', // 被调用的API版本
          partnerId: '', // 调用方的组织编码
          sysId: '', // 调用方的系统编码
          sign: '', // 请求报文的签名
        }
    const localPathValidater = (rule, value, callback) => {
      if (value.trim() === '' && this.fromData[this.activeName].storageType == STOREAGE_TYPE_LOCAL) {
        callback(new Error('本地存储路径不能为空'));
      } else {
        callback();
      }
    };
    const fileSizeValidater = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('文件传输大小上限不能为空'));
      } else if (value === '0') {
        callback(new Error('文件传输大小上限必须大于0'));
      } else if(!(/(^[1-9]\d*$)/.test(value))) {
        callback(new Error('文件传输大小上限必须为正整数'));
      }else {
        callback();
      }
    };
    
    return {
      storageTypeLocal: STOREAGE_TYPE_LOCAL,
      storageTypeS3: STOREAGE_TYPE_S3,
      currTenantCode: userInfo.tenantCode || '', //租户编码
      labelList: [
        {
          label: 'ECSB集成配置',
          key: 'ecsb'
        },
        {
          label: '主数据集成配置',
          key: 'mdm'
        },
        {
          label: '润工作集成配置',
          key: 'rwork'
        },
        {
          label: 'AI平台集成配置',
          key: 'ai'
        },
        {
          label: '消息中心管理配置',
          key: 'messageCenter'
        },
        // {
        //   label: '云文档中心集成配置',
        //   key: 'docCenter'
        // },
        {
          label: '云文档中台',
          key: 'docCenter'
        },
        {
          label: '云存储集成配置',
          key: 'cloudStorage'
        },
        {
          label: '调度管理集成配置',
          key: 'schedulingCenter'
        },
      ],
      fromDataByPublic: {
        id: '', // 新增信息，固定传空字符串
        appCode: '', // 所选行数据的应用编码字段值
        associatedApi: '', // 后端定义字段，固定传空字符串
        // ecsbParam: {}, // 后端定义字段，固定传{}
        enabled: 0, // 是否生效，1-生效，0-失效 固定传 1
        extInt: 0, // 后端定义字段，固定传0
        extLong: 0, // 后端定义字段，固定传0
        extParam: {}, // 后端定义字段，固定传{}
        extString: '', // 后端定义字段，固定传空字符串
        tenantCode: userInfo.tenantCode || '', //租户编码
        type: 'ecsb',
        settingParam: {},
        ecsbParam: ECSBParam
      },
      fromData: {
        'ecsb': {
          enabled: 0, // 是否启用
          // appToken: '', // 调用方应用授权令牌
          // appSubId: '', // 调用方的应用编码
          // apiID: '', // 被调用的API编码
          // apiVersion: '', // 被调用的API版本
          // partnerId: '', // 调用方的组织编码
          // sysId: '', // 调用方的系统编码
          // sign: '', // 请求报文的签名
          ...ECSBParam
        },
        'mdm': {
          enabled: 0, // 是否启用
          enableMdm: 0, // 开启主数据同步
          mergeMdmOrg: 0, // 合并主数据到组织架构
          synchCycle: 1, // 数据同步周期时间设置(小时)
          orgFields: [], // 组织数据
          employeeFields: [] // 员工数据
        },
        // 润工作机器人通知
        'rworkRobotNotice': {
          enabled: 0, // 是否启用
          commonEcsbAccount: true, // 延用ECSB集成配置
          appAccount: '', // 润工作应用ID
          appSecret: '', // 润工作应用Secret
          ...ECSBParam
        },
        // 润工作轻应用集成
        'rworkLightapp': {
          enabled: 0, // 是否启用
          commonEcsbAccount: true, // 延用ECSB集成配置
          appAccount: '', // 润工作应用ID
          appSecret: '', // 润工作应用Secret
          // ecsbAccount: '', // ECSB账号
          // ecsbToken: '', // ECSB Token
          // appSignSecret: '', // 应用签名密钥
          ...ECSBParam
        },
        'ai': {
          commonEcsbAccount: true, // 延用ECSB集成配置
          enabled: 0, // 是否启用
          ...ECSBParam
        },
        'messageCenter': {
          enabled: 0, // 是否启用
          appId: '', // 消息中心注册的应用id
          signCode: '', // 签名编码
          // sendType: 'MASSAGE', // 发送类别
          tenantId: '', // 租户id
          appKey: '', // 访问令牌,控制台获取
          appSecret: '', // 签名密钥
          url: '', // 消息中心服务地址
          submiter: '' // 消息中心账户ID
        },
        'cloudStorage': {
          enabled: 0, // 是否启用
          secretKey: '', // 存储密匙
          ipaddress: '', // 存储账号
          bucketName: '', // 存储空间名称
          accessKeyID: '', // 存储密码
        },
        'schedulingCenter': {
          enabled: 0, // 是否启用
          executorAppname: '', // 执行器名称
          executorIp: '', // 执行器IP
          executorLogpath: '', // 执行器日志存储路径
          executorLogretentiondays: 1, // 日志保存天数
          admiadminAddresses: '', // 调度服务地址
          accessToken: '', // 执行器通行令牌
          executorPort: '', // 执行器端口
        },
        'docCenter': {
          enabled: '', // 是否启用
          commonEcsbAccount: true, // 延用ECSB集成配置
          appId: '', // 应用账号
          appKey: '', // 应用token
          fileSize: '', // 文件传输大小上限
          unit: 'MB',
          storageType: STOREAGE_TYPE_S3, // 存储方式
          localPath: '', // 本地存储路径
          ...ECSBParam
        }
      },
      rule: {
        appToken: [{ required: true, message: '调用方应用授权令牌不能为空', trigger: "blur" }],
        appSubId: [{ required: true, message: '调用方的应用编码不能为空', trigger: "blur" }],
        // apiID: [{ required: true, message: '被调用的API编码不能为空', trigger: "blur" }],
        apiVersion: [{ required: true, message: '被调用的API版本不能为空', trigger: "blur" }],
        partnerId: [{ required: true, message: '调用方的组织编码不能为空', trigger: "blur" }],
        sysId: [{ required: true, message: '调用方的系统编码不能为空', trigger: "blur" }],
        // sign: [{ required: true, message: '请求报文的签名不能为空', trigger: "blur" }],
        appAccount: [{ required: true, message: '润工作应用账号不能为空', trigger: "blur" }],
        appSecret: [{ required: true, message: '润工作应用Secret不能为空', trigger: "blur" }],
        // ecsbAccount: [{ required: true, message: 'ECSB账号不能为空', trigger: "blur" }],
        // ecsbToken: [{ required: true, message: 'ECSB Token不能为空', trigger: "blur" }],
        // appSignSecret: [{ required: true, message: '应用签名密钥不能为空', trigger: "blur" }],
        secretKey: [{ required: true, message: '存储密匙不能为空', trigger: "blur" }],
        ipaddress: [{ required: true, message: '存储账号不能为空', trigger: "blur" }],
        bucketName: [{ required: true, message: '存储空间名称不能为空', trigger: "blur" }],
        accessKeyID: [{ required: true, message: '存储密码不能为空', trigger: "blur" }],
        executorAppname: [{ required: true, message: '执行器名称不能为空', trigger: "blur" }],
        executorIp: [{ required: true, message: '执行器IP不能为空', trigger: "blur" }],
        executorLogretentiondays: [{ required: true, message: '日志保存天数不能为空', type: "number", trigger: "blur" }],
        admiadminAddresses: [{ required: true, message: '调度服务地址不能为空', trigger: "blur" }],
        accessToken: [{ required: true, message: '执行器通行令牌不能为空', trigger: "blur" }],
        executorPort: [{ required: true, message: '执行器端口不能为空', trigger: "blur" }],

        appId: [{ required: true, message: '应用ID不能为空', trigger: "blur" }],
        signCode: [{ required: true, message: '签名编码不能为空', trigger: "blur" }],
        tenantId: [{ required: true, message: '租户ID不能为空', trigger: "blur" }],
        appKey: [{ required: true, message: '访问令牌不能为空', trigger: "blur" }],
        submiter: [{ required: true, message: '消息中心账户ID不能为空', trigger: "blur" }],
        
        fileSize: [{ required: true, validator: fileSizeValidater, trigger: "blur" }],
        localPath: [{ required: true, validator: localPathValidater, trigger: "blur" }],

      },
      activeName: 'ecsb',
      saveLoading: false,
      orgTemplateOption: [],
      staffTemplateOption: []
    }
  },
  computed: {
    isCustomByAi () {
      if (this.activeName === 'ecsb') {
        return true
      } else {
        return ['rworkRobotNotice', 'rworkLightapp', 'ai', 'docCenter'].includes(this.activeName) && !this.fromData[this.activeName].commonEcsbAccount
      }
    }
  },
  created() {
    this.$emit("SetTitle", this.$t("module.appManage.appConfig"));
    // 调整宽度
    this.$emit("SetPageWidth", 1200);
    this.fromDataByPublic.appCode = this.appInfo.clientId
    this.getConfigByType();
  },
  methods: {
    onSelect(type) {
      const selectedIds = this.fromData[this.activeName][type]
      new this.$pageModal(
        selectField,
        {
          props: {
            title: type === 'orgFields' ? '选择组织数据' : '选择员工数据',
            selected: selectedIds,
            selectList: type === 'orgFields' ? this.orgTemplateOption : this.staffTemplateOption,
          }
        },
        data => {
          if(data) {
            this.fromData[this.activeName][type] = data
          }
        }
      );
      
    },
    // 根据类型查询对应的应用配置
    getConfigByType() {
      const obj = {
        type: this.activeName,
        appCode: this.appInfo.clientId,
        tenantCode: this.currTenantCode
      }
      getAppConfigByType(obj).then(res => {
        const resData = res.data
        this.fromDataByPublic.id = resData.id || ''
        if (resData.id) {
          if (['rworkRobotNotice', 'rworkLightapp', 'ai', 'docCenter'].includes(resData.type)){
            if (!resData.ecsbParam || Object.keys(resData.ecsbParam).length === 0) {
              resData.ecsbParam = this.fromDataByPublic.ecsbParam
            }
            this.fromData[this.activeName] = {...resData.settingParam, ...resData.ecsbParam}
          } else{
            this.fromData[this.activeName] = {...this.fromData[this.activeName], ...resData.settingParam} 
          }
          this.fromData[this.activeName].enabled = resData.enabled
        }
      })
    },
    // 切换配置类型
    changeType (val) {
      if (this.activeName === val || (val === 'rwork' && ['rworkRobotNotice','rworkLightapp'].includes(this.activeName))) {
        return
      }
      this.$refs["appConfigForm"].resetFields()
      if (this.activeName === 'ai' || this.activeName === 'docCenter') {
        this.fromData[this.activeName].commonEcsbAccount = true
      }
      if (val === 'rwork') {
        this.activeName = 'rworkRobotNotice'
        this.activeNameByRwork = 'rworkRobotNotice'
      } else {
        this.activeName = val
      }
      this.fromDataByPublic.type = this.activeName
      this.getConfigByType()
      if (this.activeName === 'mdm') {
        this.getTemplate('employeefieldstemplete')
        this.getTemplate('orgfieldstemplete')
      }
    },
    // ai 是否延用ECSB集成配置
    ecsbAccountByAiChange(val) {
      if (val) {
        this.fromData[this.activeName] = {...this.fromData[this.activeName], ...this.fromDataByPublic.ecsbParam}
        // this.$refs["appConfigForm"].resetFields()
      }
    },
    // 获取模版信息
    getTemplate (id) {
      getTemplateDetail({templateId: id}).then(res => {
        const resData = res.attributeContent.template[0].options
        resData.forEach(e => {
            e.key = e.value
        })
        if (id === 'orgfieldstemplete') {
          
          this.orgTemplateOption = resData
        } else {
          this.staffTemplateOption = resData
        }
      })
    },
    onSave() {
      if (this.saveLoading) {
        return
      }
      this.saveLoading = true
      this.$refs["appConfigForm"].validate(valid => {
        if (valid) {
          const params = Object.assign({}, this.fromDataByPublic, {enabled: this.fromData[this.activeName].enabled})
          if (['rworkRobotNotice', 'rworkLightapp', 'ai', 'docCenter'].includes(this.activeName)){
            const SParam = {}
            const EParamss = {}
            for (const key in this.fromData[this.activeName]) {
              if (Object.hasOwnProperty.call(this.fromData[this.activeName], key)) {
                const element = this.fromData[this.activeName][key];
                if(this.fromDataByPublic.ecsbParam.hasOwnProperty(key)) {
                  EParamss[key] = element
                } else {
                  SParam[key] = element
                }
              }
            }
            params.settingParam = SParam
            params.ecsbParam = EParamss
          } else {
            params.settingParam = this.fromData[this.activeName]
            params.ecsbParam = {}
          }
          let submitFn = addAppConfig
          if (params.id) {
            submitFn = updateAppConfig
          }
          if (this.activeName == 'docCenter' && params.settingParam.storageType == 'amazons3') {
            getAppConfigByType({
              type: 'cloudStorage',
              appCode: this.appInfo.clientId,
              tenantCode: this.currTenantCode
            }).then(res => {
              if (res.data.enabled == 1) {
                params.settingParam.localPath = ''
                submitFn(params).then(res => {
                  this.saveLoading = false
                  this.$message.success({
                      content: params.id ? '更新成功' : '保存成功',
                      duration: 3
                    });
                }).catch(() => {
                  this.saveLoading = false
                })
              } else {
                this.saveLoading = false
                this.$message.error({
                  content: '使用S3存储，云存储集成配置必须是启用状态',
                  duration: 3
                });
              }

            }).catch(() => {
              this.saveLoading = false
            })
          } else {
            submitFn(params).then(res => {
              this.saveLoading = false
              this.$message.success({
                  content: params.id ? '更新成功' : '保存成功',
                  duration: 3
                });
            }).catch(() => {
              this.saveLoading = false
            })
          }
        } else {
          this.saveLoading = false
        }
      })
      
    }
  },
  watch: {
  }
};
</script>
<style lang="less" scoped>
.appConfig-page{
  display: flex;
  justify-content: space-between;
  height: 500px;
  .config-l{
    width: 200px;
    height: 100%;
    overflow-y: auto;
    margin-right: 6px;
    padding-right: 4px;
    ul {
      li {
        width: 100%;
        height: 48px;
        font-size: 14px;
        line-height: 48px;
        text-align: center;
        margin-bottom: 8px;
        border-radius: 4px;
        cursor: pointer;
        border: 1px solid #d8d8d8;
        &.is-active{
          font-weight: 600;
          border-color: var(--primary-color-8);
          background: var(--primary-color-8);
          color: var(--primary-color);
        }
      }
    }
  }
  .config-r{
    position: relative;
    flex: 1;
    border-radius: 4px;
    border: 1px solid #d8d8d8;
    padding: 10px;
    overflow-y: auto;
    .custom-show-only-select{
      &.ivu-select-disabled{
        /deep/.ivu-select-selection{
          background-color: #ffffff;
          cursor: pointer;
          color: #333;
        }
        /deep/.ivu-tag{
          span{
            &:not(.ivu-select-max-tag) {
              margin-right: 0;
            }
          }
          .ivu-icon-ios-close{
            display: none;
          }
        }
      }
    }
    .save-btn-box{
      // position: absolute;
      position: sticky;
      left: 0;
      bottom: -10px;
      width: 100%;
      background-color: #fff;
      height: 48px;
      line-height: 48px;
      padding: 0 10px;
      text-align: right;
      border-radius: 0 0 4px 4px;
      .ivu-btn{
        padding: 0 20px;
      }
    }
  }
}
</style>
